class UserController < ApplicationController

  def index
  end

  def online_booking
    @great_walks=Greatwalk.find_available_locations
    params[:arrival_date]=Date.today + 2
  end
  
  def update_campsites
    @resultset=[]
    @result = Campsite.find_whose_walks_id_is(params["id"])
    for results in @result
      @resultset << [results.camp_location, results.camp_location]
    end
    render :partial => 'campsites', :layout => false
  end
  
  def add_to_cart
    if !params[:children].blank?
      if ((params[:adults].to_i) + (params[:children].to_i)) > 8
        return (redirect_to_booking("Maximum 8 people in a group."))
      end 
    end
    if params[:family_claim] != "1"
      total = (((params[:adults].to_i + params[:children].to_i) * 4.85) * params[:nights].to_i)
    else
      total = (19.4 * params[:nights].to_i)
    end
    @cart=find_cart
    carts=Cart.create(:campsite => params[:camp][:id], :arrival_date => params[:arrival_date],
                     :nights => params[:nights], :adults => params[:adults],
                     :children => params[:children], :family_claim => params[:family_claim],
                     :total => total)
    if carts.save
      @cart << carts.id
      @cart_details = find_items_in_session
    else
      redirect_to_booking("Invalid input, details couldn't be stored. Please Try Again..")
    end
  end
  
  def empty_cart
    @cartitem = find_items_in_session
    empty_session_and_carts(@cartitem)
    redirect_to_index("Your cart is currently empty")
  end

  def checkout
    @customer = Customer.new
  end
  
  def checkout_details
    @customer=Customer.new(params[:customer])
    session[:cust_details]=@customer
    @cartitem=find_items_in_session
  end
  
  def save_details
    @cdet=session[:cust_details]
    session[:cust_details]=nil
    @booking_date=Date.today
    @cartitem=find_items_in_session
    if @cdet.save
      for items in @cartitem
        @camp=Campsite.find(:all, :conditions => ["camp_location = ?",items.campsite])
        for item in @camp
          @booking=Booking.create(:booking_date => @booking_date, :cust_id => @cdet.id, 
                                  :arrival_date => items.arrival_date, :nights => items.nights,
                                  :adults => items.adults, :children => items.children, 
                                  :camp_id => item.id, :total => items.total)
        end
      end
      empty_session_and_carts(@cartitem)
      redirect_to_index("Thank you for your booking")
    else
      render :action => :checkout_details
    end
  end
  
  private
  
  def find_cart
    session[:cart_ids] ||= []
  end
  
  def find_items_in_session
    Cart.find(session[:cart_ids])
  end
  
  def redirect_to_index(msg)
    flash[:notice] = msg
    redirect_to :action => :index
  end
  
  def redirect_to_booking(msg)
    flash[:notice] = msg
    redirect_to :action => :online_booking
  end
  
  def empty_session_and_carts(cartitems)
    cartitems.collect{|i| i.destroy}
    session[:cart_ids]=nil
  end
  
end
